package b4.view.component.admin
{
	import b4.config.BasicObjConfig;
	import b4.config.Config;
	import b4.config.LayerItemConfig;
	import b4.config.LayerItemConfigCreature;
	import b4.config.LayerItemConfigCreaturePos;
	import b4.config.LayerItemConfigGate;
	import b4.config.LayerItemConfigMazeObj;
	import b4.config.LayerItemConfigPort;
	
	import flash.events.Event;
	
	import org.aswing.JAdjuster;
	import org.aswing.JButton;
	import org.aswing.JComboBox;
	import org.aswing.JPanel;
	import org.aswing.JTabbedPane;
	import org.aswing.JTextField;

	public class LayerItemEditPane extends JTabbedPane
	{
		private var layerItem:LayerItemConfig;
		//传送点
		private var destTownField:JTextField;
		private var destLayerField:JTextField;
		private var destXField:JTextField;
		private var destYField:JTextField;
		//门颜色
		private var gateColorCombo:JComboBox;
		//迷宫物品索引
		private var mazeObjIndexCombo:JComboBox;
		private var mazeObjNumAdjuster:JAdjuster;
		//迷宫怪物
		private var creatureItemEditPanes:Array;
		
		public function LayerItemEditPane()
		{
			super();
			var pane:JPanel = new JPanel();
			appendTab(pane, LayerItemConfig.NAMES[0]);
			pane = new JPanel();
			appendTab(pane, LayerItemConfig.NAMES[1]);
			pane = new JPanel();
			insertPortTab(pane);
			appendTab(pane, LayerItemConfig.NAMES[2]);
			pane = new JPanel();
			insertGateTab(pane);
			appendTab(pane, LayerItemConfig.NAMES[3]);
			pane = new JPanel();
			insertMazeObjTab(pane);
			appendTab(pane, LayerItemConfig.NAMES[4]);
			pane = new JPanel();
			appendTab(pane, LayerItemConfig.NAMES[5]);
			pane = new JPanel();
			insertCreatureTab(pane);
			appendTab(pane, LayerItemConfig.NAMES[6]);
		}
		
		private function insertCreatureTab(pane:JPanel):void
		{
			creatureItemEditPanes = new Array();
			for (var i:int = 0; i < 9; i++)
			{
				var createItemEditPane:CreatureItemEditPane = new CreatureItemEditPane();
				pane.append(createItemEditPane);
				creatureItemEditPanes.push(createItemEditPane);
			}
			var saveButton:JButton = new JButton("保存");
			saveButton.addActionListener(onCreatureSaveButtonClick);
			pane.append(saveButton);
		}
		
		private function onCreatureSaveButtonClick(event:Event):void
		{
			var layerItem:LayerItemConfigCreature = layerItem as LayerItemConfigCreature;
			for (var i:int = 0; i < 9; i++)
			{
				var creatureItemEditPane:CreatureItemEditPane = creatureItemEditPanes[i];
				if (creatureItemEditPane.creatureIndexCombo.getSelectedIndex() >= 0)
				{
					var layerItemConfigCreaturePos:LayerItemConfigCreaturePos = new LayerItemConfigCreaturePos();
					var itemName:String = creatureItemEditPane.creatureIndexCombo.getSelectedItem();
					layerItemConfigCreaturePos.index = parseInt(itemName.split(" ")[0]);
					layerItemConfigCreaturePos.num = creatureItemEditPane.creatureNumAdjuster.getValue();
					layerItemConfigCreaturePos.position = creatureItemEditPane.creaturePosCombo.getSelectedIndex();
					layerItemConfigCreaturePos.isBoss = creatureItemEditPane.bossToggleButton.isSelected();
					layerItem.creatures[layerItemConfigCreaturePos.position] = layerItemConfigCreaturePos;
				}
			}
		}
		
		private function insertMazeObjTab(pane:JPanel):void
		{
			//物品索引
			var mazeObjIndexes:Array = new Array();
			var objConfig:BasicObjConfig;
			for (var objectIndexStr:String in Config.objConfigs)
			{
				objConfig = Config.objConfigs[objectIndexStr];
				if (objConfig.type == BasicObjConfig.TYPE_MAZE_OBJ)
				{
					mazeObjIndexes.push(objConfig.index);
				}
			}
			mazeObjIndexes.sort();
			var mazeObjNames:Array = new Array();
			for (var i:int = 0; i < mazeObjIndexes.length; i++)
			{
				objConfig = Config.objConfigs[mazeObjIndexes[i]];
				mazeObjNames.push(objConfig.index + " " + objConfig.name);
			}
			mazeObjIndexCombo = new JComboBox(mazeObjNames);
			mazeObjIndexCombo.setPreferredWidth(130);
			pane.append(mazeObjIndexCombo);
			//物品个数
			mazeObjNumAdjuster = new JAdjuster(3);
			pane.append(mazeObjNumAdjuster);
			var saveButton:JButton = new JButton("保存");
			saveButton.addActionListener(onMazeObjSaveButtonClick);
			pane.append(saveButton);
		}
		
		private function onMazeObjSaveButtonClick(event:Event):void
		{
			var layerItem:LayerItemConfigMazeObj = layerItem as LayerItemConfigMazeObj;
			var itemName:String = mazeObjIndexCombo.getSelectedItem();
			layerItem.objectIndex = parseInt(itemName.split(" ")[0]);
			layerItem.objectNum = mazeObjNumAdjuster.getValue();
		}
		
		private function insertGateTab(pane:JPanel):void
		{
			gateColorCombo = new JComboBox(["黄色", "红色", "蓝色"]);
			gateColorCombo.setPreferredWidth(120);
			pane.append(gateColorCombo);
			var saveButton:JButton = new JButton("保存");
			saveButton.addActionListener(onGateSaveButtonClick);
			pane.append(saveButton);
		}
		
		private function onGateSaveButtonClick(event:Event):void
		{
			var layerItem:LayerItemConfigGate = layerItem as LayerItemConfigGate;
			layerItem.color = gateColorCombo.getSelectedIndex();
		}
		
		private function insertPortTab(pane:JPanel):void
		{
			destTownField = new JTextField("", 5);			
			pane.append(destTownField);
			destLayerField = new JTextField("", 5);			
			pane.append(destLayerField);
			destXField = new JTextField("", 5);			
			pane.append(destXField);
			destYField = new JTextField("", 5);			
			pane.append(destYField);
			var saveButton:JButton = new JButton("保存");
			saveButton.addActionListener(onPortSaveButtonClick);
			pane.append(saveButton);
		}
		
		private function onPortSaveButtonClick(event:Event):void
		{
			var layerItem:LayerItemConfigPort = layerItem as LayerItemConfigPort;
			layerItem.destTownIndex = parseInt(destTownField.getText());
			layerItem.destLayer = parseInt(destLayerField.getText());
			layerItem.destX = parseInt(destXField.getText());
			layerItem.destY = parseInt(destYField.getText());
		}
		
		public function loadLayerItem(layerItem:LayerItemConfig):void
		{
			this.layerItem = layerItem;
			if (layerItem != null)
			{
				this.setSelectedIndex(layerItem.type);
				switch (layerItem.type)
				{
					case LayerItemConfig.TYPE_PORT:
						var layerItemConfigPort:LayerItemConfigPort = layerItem as LayerItemConfigPort;
						destTownField.setText(layerItemConfigPort.destTownIndex.toString());
						destLayerField.setText(layerItemConfigPort.destLayer.toString());
						destXField.setText(layerItemConfigPort.destX.toString());
						destYField.setText(layerItemConfigPort.destY.toString());
					break;
					case LayerItemConfig.TYPE_GATE:
						var layerItemConfigGate:LayerItemConfigGate = layerItem as LayerItemConfigGate;
						gateColorCombo.setSelectedIndex(layerItemConfigGate.color);
					break;
					case LayerItemConfig.TYPE_MAZE_OBJECT:
						var layerItemConfigMazeObj:LayerItemConfigMazeObj = layerItem as LayerItemConfigMazeObj;
						var objConfig:BasicObjConfig = Config.objConfigs[layerItemConfigMazeObj.objectIndex];
						mazeObjIndexCombo.setSelectedItem(objConfig.index + " " + objConfig.name);
						mazeObjNumAdjuster.setValue(layerItemConfigMazeObj.objectNum);
					break;
					case LayerItemConfig.TYPE_CREATURE:
						var layerItemConfigCreature:LayerItemConfigCreature = layerItem as LayerItemConfigCreature;
						var count:int = 0;
						for (var posStr:String in layerItemConfigCreature.creatures)
						{
							var layerItemConfigCreaturePos:LayerItemConfigCreaturePos = layerItemConfigCreature.creatures[posStr];
							var creatureItemEditPane:CreatureItemEditPane = creatureItemEditPanes[count++];
							objConfig = Config.objConfigs[layerItemConfigCreaturePos.index];
							creatureItemEditPane.creatureIndexCombo.setSelectedItem(objConfig.index + " " + objConfig.name);
							creatureItemEditPane.creatureNumAdjuster.setValue(layerItemConfigCreaturePos.num);
							creatureItemEditPane.creaturePosCombo.setSelectedIndex(layerItemConfigCreaturePos.position);
							creatureItemEditPane.bossToggleButton.setSelected(layerItemConfigCreaturePos.isBoss);
						} 
					break;
				}
			}
		}
		
	}
}